<?php
require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/init.php';

/**
 *向dms获取销量数据
 */
class SynSalesController extends Controller_CronJob {
	//dms数据库连接对象
	private $dmsSystemDb;

	public function __construct() {
		parent::__construct();
		$this->dmsSystemDb = Model_Db::getInstance('dms_db');
	}

	public function run() {
		//清空我们数据库的销量日志表
		$this->writeDb->query('CALL SET_SOLDS_DATA_INIT()');
		$DbModel = new Model_Base();

		$everyTimeHandleNum = 40000;
		$offset = 0;
		$times = 0;
		$sql = 'SELECT goods_sn, pay_date sold_date, goods_count sold_amount, stock_code warehouse_code FROM public_dm_goods_stock_sale LIMIT ';
		do {
			if (count($result = $this->dmsSystemDb->fetchAll($sql . $offset . ', ' . $everyTimeHandleNum)) == 0) {
				break;
			}
			$DbModel->insertBatch($this->writeDb, 'p_product_sales_log', $result);
			$times++;
			$offset = $everyTimeHandleNum * $times;
		} while (1);
	}
}

$synSales = new SynSalesController();
$synSales->run();